package com.gaohe.config;

import com.gaohe.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.List;

@Configuration
@EnableWebMvc
@Slf4j
public class SpringMvcConfig implements WebMvcConfigurer {
    //静态资源放行
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.addResourceHandler("/back/**")
               .addResourceLocations("classpath:/back/");
       log.info("静态资源放行");
    }
    //登录拦截器
   @Autowired
   private LoginInterceptor loginInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
       registry.addInterceptor(loginInterceptor).addPathPatterns("/**");
    }
//自定义消息转化器配置
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        //默认的json转换的转换器
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        //把自定义的转换格式覆盖掉原来的格式
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //把自定义转化器加入到mvc容器中，第一顺位执行
        converters.add(0,messageConverter);


    }
}
